package com.powernode.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.powernode.domain.Basket;
import com.powernode.domain.Prod;
import com.powernode.service.BasketService;
import com.powernode.utils.PowerAuthMenuUtil;
import com.powernode.vo.CartMoney;
import com.powernode.vo.CartVo;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.List;


@RestController
@RequestMapping("p/shopCart")
@Api(tags = "购物车接口")
public class CartController {
    @Autowired
    private BasketService basketService;

    @GetMapping("/info")
    @ApiOperation("购物车数据获取")
    //使用封装的对象
    public ResponseEntity<CartVo> basketInfo() {
        //根据用户id
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        CartVo cartVo = basketService.basketInfo(wxUserId);
        return ResponseEntity.ok(cartVo);
    }

    @GetMapping("/prodCount")
    @ApiOperation("用户购物车商品个数")
    public ResponseEntity<Integer> prodCount() {
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        int count = basketService.count(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getUserId, wxUserId));
        return ResponseEntity.ok(count);
    }

    /**
     * 加入购物车需要先对购物车容量进行判断
     *
     * @param basket
     * @return
     */
    @PostMapping("/changeItem")
    @ApiOperation("加入购物车和修改数量")
    public ResponseEntity<Void> changeItem(@RequestBody Basket basket) {
        String wxUserId = PowerAuthMenuUtil.getWxUserId();
        basketService.changeItem(basket, wxUserId);
        return ResponseEntity.ok().build();
    }


    @PostMapping("/totalPay")
    @ApiOperation("用户购物车勾选商品的价格")
    public ResponseEntity<CartMoney> totalPay(@RequestBody List<Long> basketIds) {
        CartMoney cartMoney = basketService.totalPay(basketIds);
        return ResponseEntity.ok(cartMoney);
    }

    @PostMapping("/loadBasketById")
    @ApiOperation("根据basketId获取商品")
    List<Basket> loadProdById(@RequestBody List<Long> basketIds) {
        return basketService.listByIds(basketIds);
    }

    @PostMapping("/removeCartById/{wxUserId}")
    @ApiOperation("删除购物车信息")
   public void removeCartById(@RequestBody ArrayList<Long> skuIds, @RequestParam String wxUserId) {
        basketService.remove(new LambdaQueryWrapper<Basket>()
                .eq(Basket::getUserId, wxUserId)
                .in(Basket::getSkuId, skuIds));
    }
}
